package com.gamota.mobi;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.androidquery.AQuery;

import com.devspark.appmsg.AppMsg;
import com.gamota.mobi.adapter.ErrorHandler;
import com.gamota.mobi.background.BackgroundTasks;
import com.gamota.mobi.event.Event.GetFullItemInfoFail;
import com.gamota.mobi.event.Event.GetFullItemInfoSuccessEvent;
import com.gamota.mobi.event.Event.RequestFailEvent;
import com.gamota.mobi.util.Constants;
import com.gamota.mobi.util.Utils;

import de.greenrobot.event.EventBus;

public class ItemBrokerActivity extends Activity {
	
	private String packageName;
	private String slug;
	private String previousScreen = "";
	private int autoInstall = 0;
	private String referer = "";
	private BackgroundTasks bgTask;
	private EventBus eventBus;
	private AQuery aq;
	private String store;
	private int from;
	private String countryCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_broker);
		
		bgTask = new BackgroundTasks(this);
		eventBus = EventBus.getDefault();
		aq = new AQuery(this);
		packageName = getIntent().getStringExtra(Constants.PACKAGE_NAME);
		if(null == packageName){
			packageName = "";
		}
		store = getIntent().getStringExtra(Constants.STORE_KEY);
		slug = getIntent().getStringExtra(Constants.SLUG);
		from = getIntent().getIntExtra(Constants.FROM, 0);
		previousScreen = getIntent().getStringExtra(Constants.PREVIOUS_SCREEN);
		autoInstall = getIntent().getIntExtra(Constants.AUTO_INSTALL, 0);
		if(null == previousScreen){
			previousScreen = "";
		}
		referer = getIntent().getStringExtra(Constants.KEY_REFERER);
		if(null == referer){
			referer = "direct";
		}
		countryCode = Utils.getStaticCountryCode(this);
		if(countryCode.equalsIgnoreCase("vn")){
			loadData();
		} else {
			String url = "market://details?id=" + packageName;
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(url));
			startActivity(i);
		}
	}

	private void loadData(){
		aq.id(R.id.broker_prg).visible();
		aq.id(R.id.broker_retry).gone();
		aq.id(R.id.empty_view).gone();
		String accessToken = Utils.getStaticAccessToken(this);
		if(packageName==null||packageName.equalsIgnoreCase("null")){
			packageName="";
		}
		bgTask.getFullItemInfo(accessToken, slug, packageName, referer);
	}
	
	public void onEventMainThread(GetFullItemInfoSuccessEvent event){
		Intent intent = new Intent(this, DetailsActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable(Constants.ITEM_INFO, event.getItem());
		bundle.putString(Constants.PREVIOUS_SCREEN, previousScreen);
		bundle.putInt(Constants.AUTO_INSTALL, autoInstall);
		bundle.putString(Constants.KEY_REFERER, referer);
		bundle.putString(Constants.STORE_KEY, store);
		bundle.putInt(Constants.FROM, from);
		intent.putExtras(bundle);
		startActivity(intent);
		finish();
	}
	
	public void onEventMainThread(GetFullItemInfoFail e){
		if(e.getMessage().contains("exist")){
			aq.id(R.id.broker_prg).gone();
			aq.id(R.id.broker_retry).gone();
			aq.id(R.id.empty_view).visible();
		} else {
			AppMsg.makeText(this, e.getMessage(), AppMsg.STYLE_ALERT, null).show();
			aq.id(R.id.broker_prg).gone();
			aq.id(R.id.broker_retry).visible().clicked(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					loadData();
				}
			});;
			aq.id(R.id.empty_view).gone();
		}
	}

	public void onEventMainThread(RequestFailEvent event){
		ErrorHandler.getInstance().setContext(this).handleError(event.getErrorCode());
		aq.id(R.id.broker_prg).gone();
		aq.id(R.id.empty_view).gone();
		aq.id(R.id.broker_retry).visible().clicked(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				loadData();
			}
		});
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		eventBus.register(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		eventBus.unregister(this);
	}
	
}
